package org.hgvc.webapp.ets.jsf.beans.member;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.bo.SelectObj;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MembershipService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;
import org.jocsb.base.util.SpringUtils;


public class MemberProductGiftSelectMembershipBean extends AbstractBean{

	public String BEAN_NAME="memberProductGiftSelectMembershipBean";
	private MembershipService service;
	private List<Membership>membershipList;
	private String NAV_SELECT_ACTION="memberSelectMembershipAsGift";
	private String NAV_SELECT_MEMBER_TYPE="memberSelectMemberTypeAsGift";
	private Membership membership;
	private List<SelectObj> typeList;
	private String selectedType;

	public void setService(MembershipService service) {
		this.service = service;
	}
	/*-----------------------------------------------------*/

	public String navListAction(){
		return NAV_SELECT_MEMBER_TYPE;
	}
	
	public List<SelectObj> getTypeList() {

		SelectObj indMember = new SelectObj();
		indMember.setId(CommonConstant.MEMBER_TYPE_IND);
		indMember.setTitle(SpringUtils.getMessage(CommonConstant.MEMBER_TYPE_IND, new Object[]{}));

		SelectObj corMember = new SelectObj();
		corMember.setId(CommonConstant.MEMBER_TYPE_COMP);
		corMember.setTitle(SpringUtils.getMessage(CommonConstant.MEMBER_TYPE_COMP, new Object[]{}));

		typeList = new ArrayList<SelectObj>();
		typeList.add(indMember);
		typeList.add(corMember);

		return typeList;
	}

	public void selectActionEvent(ActionEvent actionEvent){
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("membershipId");
		String id = (String)param.getValue();
		for(Membership membership:membershipList){
			if(membership.getMembershipId().equalsIgnoreCase(id)){
				this.membership = membership;
				break;
			}
		}
	}

	public String navProspectCreation(){
		try{
			if(selectedType.equalsIgnoreCase(CommonConstant.MEMBER_TYPE_IND)){
				MemberIndProspectProductAsGift bean = (MemberIndProspectProductAsGift)FacesUtils.getManagedBean(MemberIndProspectProductAsGift.BEAN_NAME);
				bean.setMembership(membership);
				return bean.navCreateAction();
			}else{
				MemberCompProspectProductAsGift bean = (MemberCompProspectProductAsGift)FacesUtils.getManagedBean(MemberCompProspectProductAsGift.BEAN_NAME);
				bean.setMembership(membership);
				return bean.navCreateAction();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<Membership> getMembershipList() {
		try{
			membershipList = service.getMembershipList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return membershipList;
	}
	
	public void createMemberListener(ActionEvent actionevent){
		UIParameter param = (UIParameter)actionevent.getComponent().findComponent("memberType");
		selectedType = (String)param.getValue();
	}
	

	public String navCreateMember(){
		return NAV_SELECT_ACTION;
	}
}
